import os
import sys
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler


class ReloadHandler(FileSystemEventHandler):
    def __init__(self, script):
        self.script = script

    def on_modified(self, event):
        if event.src_path.endswith('.py'):
            AntLogger.info(f"{event.src_path} 文件修改，重新启动程序")
            os.execv(sys.executable, ['python'] + [self.script])


if __name__ == "__main__":
    script = "main.py"  # 替换为你的 PyQt 程序的脚本名称
    event_handler = ReloadHandler(script)
    observer = Observer()
    observer.schedule(event_handler, path='.', recursive=True)
    observer.start()

    try:
        os.system(f"python {script}")
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
